<?php
    /**
    * @author: trungdt 
    * @since Jun 23, 2011
    * Change logs
    */

    class bdsmodule {        
        function name() { return "Management real estate"; }
        function description() { return "Management real estate"; }
        function author() { return "ABSoft.vn"; }

        function hasSources() { return true; }
        function hasContent() { return true; }
        function hasViews() { return true; }

        function supportsWorkflow() { return false; }

        function permissions($internal = "") {
            return array(
            'administrate'=>'Administrate',
            'configure'=>'Configure',
            'create'=>'Create',
            'edit'=>'Edit',
            'delete'=>'Delete'
            );
        }

        function show($view,$loc = null, $title = "") {
            // list all project (for management only - hehe)
            $_REQUEST['page']=1;
            $_REQUEST['flow_has_set']=1; 
            exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_SECTIONAL);
            include("actions/list.php");
        }

        function deleteIn($loc) {
            global $db;
            $db->delete('linklist_link',"location_data='".serialize($loc)."'");
        }

        function copyContent($oloc,$nloc) {
            foreach ($db->selectObjects('linklist_link',"location_data='".serialize($oloc)."'") as $l) {
                $l->location_data = serialize($nloc);
                $db->insertObject($l,'linklist_link');
            }
        }

        // No search support
        function searchName() {
            return false;
        }
        
        /**
        * Process all project
        */
        static function _bdsProcess($projects){
            global $db;
            
            foreach ($projects as $k=>$project)
            {
                // date
                $project->date_full=_ab_dateFormat($project->created_on,0);
                $project->hethan_full=_ab_dateFormat($project->hethan,0);
                
                // for thumbnail
                if ($project->thumbnail <= 0) {
                    $project->thumbnail_path = '';
                } else {
                    $file = $db->selectObject('file', 'id='.$project->thumbnail);
                    $project->thumbnail_path = BASE_URL."/".$file->directory.'/'.$file->filename;
                }
                
            }
            
            return $projects;
        }
        

        /**
        * Generate sub menu for menu Du an
        * 
        * @param mixed $parent_section Section Du an
        */
        static function MenuGenerate_ForCat($parent_section, $sec_name, $col_type){
            // copy section du an
            global $db;
            $obj=null;
            $project_menu_id="";
            $save_last_array_part=array();
            $params=array();
            foreach ($parent_section as $k=>$v)
            {
                if ($v->sef_name == $sec_name) // FOOL . hehe
                {
                    // change URL of this object first, hehe
                     $params['module']="bdsmodule";
                     $params['action']="view";
                     $params['action_type']=$sec_name;
                     $params['src']="@random4e0d70a6d793d";
                     $v->link=exponent_core_makeLink($params);
                    
                    
                    $obj = clone $v;
                    $project_menu_id=$v->id;
                    $temp=array_chunk($parent_section,$k+1); // we split into 2 part hehe
                    if (count($temp)>1)
                        $save_last_array_part=$temp[1];
                    $parent_section=$temp[0];
                    break;
                }
            }

            //
            $cats=$db->selectObjects("bds_collections"," col_type={$col_type} ORDER BY rank");
            $params=array();
            foreach ($cats as $k=>$v)
            {
                $obj->id=$v->id*rand(1,10);
                $obj->name=$v->name;
                $obj->sef_name=$v->shortname;
                $params['module']="bdsmodule";
                $params['action']="view";
                $params['action_type']=$sec_name;
                $params['loai_bds']=$v->shortname;
                $temp=unserialize($v->location_data);
                $params['src']=$temp->src;
                $obj->link=exponent_core_makeLink($params);
                $obj->depth=1;
                $obj->parents=array($project_menu_id);
                $parent_section[]=clone $obj;
            }

            $parent_section=array_merge($parent_section, $save_last_array_part);
            return $parent_section;
        }

        /**
        * Get Project Collection By ID or by Shortname
        * 
        * @param int $id
        */
        static function _GetProjectCol_byID($id)
        {
            $sql_where="";
            global $db;

            if (is_int($id))
            {
                $id = intval($id);
                $sql_where=" id = {$id} ";
            }
            else
            {
                $id = _ab_safe_html_string($id);
                $sql_where=" shortname = '{$id}' ";
            }

            $temp=$db->selectObject("duan_collections"," {$sql_where} ");

            if ($temp)
            {
                return $temp;
            }
            return -1; // in case search not found, return -1
        }
    }

?>
